package org.budo.event.multicaster.executor;

import java.util.List;
import java.util.concurrent.ExecutorService;

import org.budo.event.BudoEvent;
import org.budo.event.BudoEventListener;
import org.budo.event.multicaster.AbstractEventMulticaster;

/**
 * @author lmw
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
public class ExecutorServiceEventMulticaster extends AbstractEventMulticaster {
    private ExecutorService executorService;

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public void setExecutorService(ExecutorService executorService) {
        this.executorService = executorService;
    }

    public void multicastEvent(final BudoEvent event) {
        List<BudoEventListener> eventListeners = this.retrieveEventListeners(event);

        for (final BudoEventListener eventListener : eventListeners) {
            this.getExecutorService().execute(new Runnable() {
                public void run() {
                    eventListener.onEvent(event);
                }
            });
        }
    }
}